#include "bsp.h"
#include "built_effect.h"
#include "app_key.h"

void SystemClockConfig(void);
static uint16_t led_tick = 0;

void led_task(void)
{

    if (led_tick >= 500) {
        led_tick = 0;
        bsp_led_toggle();
    }

}

void sys_tick(void) {
    led_tick++;
    app_key_tick_1ms();
}

int main(void)
{
    SystemClockConfig();
    NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_3);

    bsp_ticktim_init();
    bsp_ticktim_cbf_regist(sys_tick); 
    
    bsp_debug_uart_config();
    SYSTEM_SHOW_INFO();
    
    ws2812_init();
    bsp_led_init();

    app_key_init();

    while (1)
    {
        led_task();
        ws2812_test();
        LL_mDelay(50);
    }
}

void SystemClockConfig(void)
{
    /* Enable HSE */
    LL_RCC_HSE_SetFreqRegion(LL_RCC_HSE_4_8MHz);

    LL_RCC_HSE_Enable();
    while (LL_RCC_HSE_IsReady() != 1)
    {
    }
    /* Configure PLL */
    LL_RCC_PLL_Disable();
    while (LL_RCC_PLL_IsReady() != 0)
    {
    }
    LL_RCC_PLL_SetMainSource(LL_RCC_PLLSOURCE_HSE);
    LL_RCC_PLL_SetMulFactor(LL_RCC_PLLMUL_16);
    LL_RCC_PLL_Enable();
    while (LL_RCC_PLL_IsReady() != 1)
    {
    }
    /* Set flash latency */
    LL_FLASH_SetLatency(LL_FLASH_LATENCY_6);
    while (LL_FLASH_GetLatency() != LL_FLASH_LATENCY_6)
    {
    }

    /* Select PLL as system clock source, and if it goes over or under 80Mhz,
    it is necessary to use HCLK prescaler 2 for intermediate steps */
    LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_2);
    LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);

    LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
    LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
    LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);

    SystemCoreClockUpdate();
    LL_Init1msTick(SystemCoreClock);

    LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
    LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);

}


/**
 * @brief  Error handling function
 * @param  None
 * @retval None
 */
void APP_ErrorHandler(void)
{
    /* Infinite loop */
    while (1)
    {
    }
}

#ifdef USE_FULL_ASSERT
/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file：Pointer to the source file name
 * @param  line：assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* User can add His own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* Infinite loop */
    while (1)
    {
    }
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT Puya *****END OF FILE******************/
